﻿// FirstWindow.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#if 1

#include <iostream>
#include "src/MyGLUtils.h"
#include "src/Shader.h"
#include "src/MyUtils.h"
#include "src/ModelLoader.h"
#include "src/App.h"

//#include <UnityMath.h>
//#pragma comment (lib,"UnityMathLib.lib" )  


int main()
{
    glfwSetErrorCallback(App::GLFWErrorCallback);

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    // 选择  opengl 版本和 context
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    /* Create a windowed mode window and its OpenGL context */
    auto& gApp = App::instance();
    GLFWwindow* window = glfwCreateWindow(gApp.width, gApp.height, gApp.title.c_str(), NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }
    gApp.window = window;

    /* Make the window's context current */
    glfwMakeContextCurrent(window);
    glfwSwapInterval(1); // Enable vsync


#pragma region 初始化glew
    GLenum err = glewInit();
    if (GLEW_OK != err)
    {
        /* Problem: glewInit failed, something is seriously wrong. */
        fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
        exit(1);
    }
    fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
#pragma endregion

    // init gl
    // 打印gl版本
    fprintf(stdout, "gl version:%s\n", glGetString(GL_VERSION));
    fprintf(stdout, "gl vendor:%s\n", glGetString(GL_VENDOR));

    gApp.Init();

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        glfwPollEvents();


        // read input
        gApp.Update();

        /* Render here */
        gApp.Render();
        glfwSwapBuffers(window);
    }
    glfwDestroyWindow(window);
    glfwTerminate();
}


#endif // 0